﻿
@{
    Layout = null;
    string guid = "g" + Guid.NewGuid().ToString().Replace("-", "");
}
<style>
    .modal-dialog {
        width: 500px;
    }
</style>
<div  id="@guid" style="overflow-y:scroll;max-height:400px;">
    <form action="@Url.Action("AuthorizationSave")" class="form-horizontal">
        @Html.AntiForgeryToken()
        <input type="hidden" id="roleId" name="roleId" value="@ViewBag.roleId" />
        <input type="hidden" id="moduleIds" name="moduleIds" value="" />
        <div class="ztree" id="AuthorizationTree@(guid)">

        </div>
    </form>

</div>
<script>
    $(function () {
        var moduleId = 0;
        var level = 0;
        var setting = {
            check: {
                enable: true
            },
            callback: {
                onClick: function (e, treeId, treeNode) {
                    moduleId = treeNode.id;
                    level = treeNode.level + 1;
                },
                onCheck: function (e, treeId, treeNode) {
                    var zTree = $.fn.zTree.getZTreeObj("AuthorizationTree@(guid)");
                    var nodes = zTree.getCheckedNodes(true);
                    var ids = "";
                    for (var i = 0; i < nodes.length; i++) {
                        var id = nodes[i].id;
                        if (id == 0) {
                            continue;
                        }
                        ids += "," + id;
                    }
                    $("#@(guid) #moduleIds").val(ids);
                }
            }
        };
        var zNodes = [];
        ajaxGet("/SysRole/AuthorizationTree/@ViewBag.roleId", null, function (data) { 
             zNodes = data;
            var tree = $.fn.zTree.init($("#AuthorizationTree@(guid)"), setting, zNodes);
            //默认选中第一个节点，并点击它
            var node = tree.getNodes()[0];
            tree.selectNode(node);
            setting.callback.onClick(null, tree.setting.treeId, node);

        },"json");
       

    });
</script>
